import axios from 'axios'
import { useUserStore } from '@/stores/user'
import { pinia } from '@/main' // 导入main.js导出的实例

const request = axios.create({
    baseURL: 'http://127.0.0.1:8080',
    timeout: 20000
})

request.interceptors.request.use(config => {
    const userStore = useUserStore(pinia) // 使用正确的实例
    if (userStore?.token) {
        config.headers.Authorization = `Bearer ${userStore.token}`
    }
    return config
})

export default request